// Toast.qml
import QtQuick 2.15
import QtQuick.Controls 2.15

Popup {
    id: toast

    property string text: ""
    property int duration: 3000
    
    x: parent ? (parent.width - width) / 2 : 0
    y: parent ? parent.height - height - 50 : 0
    
    padding: 12
    width: Math.min(contentLabel.implicitWidth + 24, parent ? parent.width - 40 : 200)
    
    // 自动关闭计时器
    Timer {
        id: hideTimer
        interval: duration
        onTriggered: close()
    }
    
    background: Rectangle {
        color: "#CC000000"
        radius: 6
    }
    
    contentItem: Text {
        id: contentLabel
        text: toast.text
        color: "white"
        font.pixelSize: 14
        horizontalAlignment: Text.AlignHCenter
        wrapMode: Text.Wrap
    }
    
    // 显示方法
    function show(message, showDuration) {
        toast.text = message;
        if (showDuration !== undefined) duration = showDuration;
        open();
        hideTimer.restart();
    }
    
    // 动画完成信号
    signal animationFinished()
    
    onClosed: {
        animationFinished();
    }
}